package entity;

import loading.ImageLoader;
import main.GameList;
import math.Equations;
import math.Vector2D;

import java.awt.geom.*; 
import java.awt.*;

import entity.immobile.Door;

/**
 * @author Greg G
 * 
 * The entity class is the superclass for all objects in the game
 * Any static entity will directly extend this, and dynamic entities
 * will extend DynamicEntity, a subclass of Entity. Entity has methods
 * and variables for positioning and rotating objects.
 */
public class Entity 
{
	private ImageLoader image;
	
	private Point2D position;
	
	private double angVel; //used to update the angle
	private double angle;
	private double radius;
	
	private boolean circular; //determines if circular collision is preferred
	
	//the game data
	private GameList game;
	
	
	public Entity(Point2D pos, GameList game, ImageLoader img)
	{
		position = pos;
		this.game = game;
		image = img;
		radius = img.getImage().getWidth()/2;
		angle = 0;
		angVel = 0;
		circular = true;
	}
	
	public Rectangle2D.Double getBounds()
	{
		return new Rectangle2D.Double(position.getX() - image.width()/2,
									  position.getY() - image.height()/2,
									  image.width(),
									  image.height());
	}
	
	public void update(double duration)
	{
		angle += angVel * duration;	
		//first, fix it between 0 and 2PI
		if(angle > 2*Math.PI)
			angle = angle % (2*Math.PI);
		if(angle < 0)
			angle = 2*Math.PI - angle % (2*Math.PI);
	}
	public void setAngularVelocity(double angVel)
	{
		this.angVel = angVel;
	}
	public double getAngularVelocity()
	{
		return angVel;
	}
	public void setAngle(double ang)
	{
		angle = Equations.normalizeAngle(ang);
	}
	public double getAngle()
	{
		return angle;
	}
	public void setPosition(double x, double y)
	{
		position.setLocation(x, y);
	}
	public void setPosition(Point2D v)
	{
		position = v;
	}
	public Point2D getPosition()
	{
		return position;
	}
	public void setRadius(double rad)
	{
		radius = rad;
	}
	public double getRadius()
	{
		return radius;
	}
	public int getHeight()
	{
		return image.height();
	}
	public int getWidth()
	{
		return image.width();
	}
	public GameList getGameList() 
	{
		return game;
	}
	
	protected ImageLoader getImage()
	{
		return image;
	}
	
	public boolean isCircular() 
	{
		return circular;
	}

	public void setCircular(boolean circular) 
	{
		this.circular = circular;
	}

	//draw the entity to the graphics object
	public void draw(Graphics2D g)
	{
		AffineTransform at = new AffineTransform();
		at.rotate(angle, position.getX(), position.getY());
		if (circular)
		{
			at.translate(position.getX() - image.width()/2, position.getY() - image.height()/2);
		}
		else
		{
			at.translate(position.getX(), position.getY());
		}
		//draw the image based on the rotation and loaded image
		image.draw(g, at);
	}
}
